package org.example.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.springcloud.entities.Payment;
import org.example.springcloud.result.CommonResult;
import org.example.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Jandmla
 * Date: 2022/7/22
 * Time: 14:15
 * Description:
 */
@RestController
@Slf4j
public class PaymentControllerImpl implements PaymentController{

    private final PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    public PaymentControllerImpl(PaymentService paymentService){
        this.paymentService = paymentService;
    }

    /**
     * 数据插入
     * @param payment 数据实体
     * @return 插入结果
     */
    @PostMapping(value = "/payment/create")
    public CommonResult<Integer> create(@RequestBody Payment payment){
        int insert = paymentService.create(payment);
        log.info("{}/payment/create    \t  create(Payment payment) ====> result = {}",serverPort,insert);
        if (insert > 0){
            return new CommonResult<>(200,"数据插入成功！  serverPort:  "+ serverPort,insert);
        }else {
            return new CommonResult<>(500,"操作失败！  serverPort:  "+ serverPort);
        }
    }

    /**
     * 数据查询
     * @param id ID
     * @return 查询结果
     */
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> get(@PathVariable("id") Long id){
        Payment result = paymentService.getPaymentById(id);
        log.info("/payment/get       \t  get(Long id) ====> result = {}",result.toString());
        if (null != result.getId()){
            return new CommonResult<>(200,"数据查询成功！  serverPort:  "+ serverPort,result);
        }else {
            return new CommonResult<>(450,"数据查询失败，可能没有该数据！  serverPort:  "+ serverPort);
        }
    }
}